package com.example.test.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tencent.weibo.sdk.android.model.ModelResult;


public class QQJsonAnalyzer {

	public static List<HashMap<String, String>> OneWeiboJsonToHasArr(ModelResult result)
	{
		if(!result.isSuccess())
		{
			return null;
		}
		
        try {
        	JSONObject object =(JSONObject) result.getObj();
			List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
			HashMap<String, String> map =new HashMap<String, String>();
			JSONObject firstObj =object.getJSONObject("data");
			String beginDate=firstObj.getString("timestamp")+"000";

            SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",new Locale("en"));

            String sd = sdf.format(new Date(Long.parseLong(beginDate)));
			map.put("id", firstObj.getString("id"));
            map.put("resource", "QQ");
            map.put("created_at", sd);
            map.put("userscreen_name", firstObj.getString("nick"));
            map.put("source",">"+firstObj.getString("from")+"</a>");
            String text =firstObj.getString("text");
            text=processUrl(text);
            map.put("text",text );
            map.put("reposts_count",firstObj.getString("count"));
    		map.put("comments_count",firstObj.getString("mcount"));
    		map.put("attitudes_count","0");
    		map.put("userprofile_image_url",firstObj.getString("head")+"/40");
    		if(firstObj.getInt("type")==2)
            {
         	   map.put("retweeted_statususerscreen_name", firstObj.getJSONObject("source").getString("name"));
         	   String retweeted_statustext = firstObj.getJSONObject("source").getString("text");
         	   retweeted_statustext =processUrl(retweeted_statustext);
         	   map.put("retweeted_statustext", retweeted_statustext);
         	   map.put("retweeted_statusid", firstObj.getJSONObject("source").getString("id"));
         	   String returls="";
         	   if(!firstObj.getJSONObject("source").getString("image").equals("null"))
         		{
         		JSONArray array = firstObj.getJSONObject("source").getJSONArray("image");
         		for(int j=0;j<array.length();j++)
         		{
         			String testurl=array.getString(j);
         			testurl=testurl.substring(0,testurl.length());
         			testurl=testurl.replaceAll("\\\\", "");
         			returls+=testurl+"/2000,";
         		}
         		if(array.length()>0)
         			returls=returls.substring(0,returls.length()-1);
         		
         		}
         		map.put("retweeted_statusimages", returls);
            }
    		String urls="";
       		if(!firstObj.getString("image").equals("null"))
       		{
       		JSONArray array = firstObj.getJSONObject("pic").getJSONArray("info");
       		for(int j=0;j<array.length();j++)
       		{
       			String testurl=array.getJSONObject(j).getString("url");
       			testurl=testurl.substring(2,testurl.length()-2);
       			testurl=testurl.replaceAll("\\\\", "");
       			urls+=testurl+"/2000,";
       		}
       		if(array.length()>0)
       			urls=urls.substring(0,urls.length()-1);
       		
       		}
       		map.put("images", urls);
    		
			listArrays.add(map);
			return listArrays;
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        return null;
	}
	
	public static List<HashMap<String, String>> userJsonToHasArr(ModelResult result)
	{
		if(!result.isSuccess())
		{
			return null;
		}
		
        try {
        	JSONObject object =(JSONObject) result.getObj();
			List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
			HashMap<String, String> map =new HashMap<String, String>();
			JSONObject firstObj =object.getJSONObject("data");
			map.put("name", firstObj.getString("nick"));
			map.put("followers_count", firstObj.getString("fansnum"));
			map.put("friends_count", firstObj.getString("idolnum"));
			map.put("statuses_count", firstObj.getString("tweetnum"));
			map.put("avatar_large", firstObj.getString("head")+"/100");
			listArrays.add(map);
			return listArrays;
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        return null;
	}
	
	public static List<HashMap<String, String>> friendsJsonToHasArr(ModelResult result)
	{
		if(!result.isSuccess())
		{
			return null;
		}
		
        try {
        	JSONObject object =(JSONObject) result.getObj();
			List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
			
			JSONArray array =object.getJSONObject("data").getJSONArray("info");
			for(int i=0;i<array.length();i++)
			{
				HashMap<String, String> map =new HashMap<String, String>();
				map.put("name", array.getJSONObject(i).getString("nick"));
				map.put("description", "");
				if(array.getJSONObject(i).getString("head").trim().equals(""))
					map.put("profile_image_url","");
				else
				map.put("profile_image_url", array.getJSONObject(i).getString("head")+"/100");
				map.put("uid", array.getJSONObject(i).getString("openid"));
				map.put("following",array.getJSONObject(i).getString("isidol"));
				map.put("resource","QQ");
				listArrays.add(map);
				
			}
			
			
			
			return listArrays;
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        return null;
	}
	
	public static List<HashMap<String, String>> timeLineJsonToHasArr(ModelResult result)
   {
		if(!result.isSuccess())
		{
			return null;
		}
		int f=0;
	   try {  
		   JSONObject object =(JSONObject) result.getObj();
		   if(object.getString("data").equals("null"))
			   return null;
           JSONArray jsonArrays = ((JSONObject)object.get("data")).getJSONArray("info");  
           List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
           
           for (int i = 0; i < jsonArrays.length(); i++) {  
        	   f++;
               JSONObject jsonObject = jsonArrays.getJSONObject(i);  
               HashMap<String, String> map = new HashMap<String, String>(); 
               String beginDate=jsonObject.getString("timestamp")+"000";

               SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",new Locale("en"));

               String sd = sdf.format(new Date(Long.parseLong(beginDate)));
               if(jsonObject.getInt("type")==2)
               {
            	   map.put("retweeted_statususerscreen_name", jsonObject.getJSONObject("source").getString("name"));
            	   String retweeted_statustext = jsonObject.getJSONObject("source").getString("text");
            	   retweeted_statustext =processUrl(retweeted_statustext);
            	   map.put("retweeted_statustext", retweeted_statustext);
            	   map.put("retweeted_statusid", jsonObject.getJSONObject("source").getString("id"));
            	   String returls="";
             	   if(!jsonObject.getJSONObject("source").getString("image").equals("null"))
             		{
             		JSONArray array = jsonObject.getJSONObject("source").getJSONArray("image");
             		for(int j=0;j<array.length();j++)
             		{
             			String testurl=array.getString(j);
             			testurl=testurl.substring(0,testurl.length());
             			testurl=testurl.replaceAll("\\\\", "");
             			returls+=testurl+"/2000,";
             		}
             		if(array.length()>0)
             			returls=returls.substring(0,returls.length()-1);
             		
             		}
             		map.put("retweeted_statusimages", returls);
               }
               map.put("id", jsonObject.getString("id"));
               map.put("resource", "QQ");
               map.put("created_at", sd);
               map.put("userscreen_name", jsonObject.getString("nick"));
               map.put("userid", jsonObject.getString("openid"));
               map.put("source",">"+jsonObject.getString("from")+"</a>");
               String text=jsonObject.getString("text");
               text = processUrl(text);
               map.put("text",text );
               map.put("reposts_count",jsonObject.getString("count"));
       		map.put("comments_count",jsonObject.getString("mcount"));
       		map.put("attitudes_count","0");
       		map.put("timestamp",jsonObject.getString("timestamp")+"000");
       		map.put("userprofile_image_url",jsonObject.getString("head")+"/40");
       		
       		String urls="";
       		if(!jsonObject.getString("image").equals("null"))
       		{
       		JSONArray array = jsonObject.getJSONObject("pic").getJSONArray("info");
       		for(int j=0;j<array.length();j++)
       		{
       			String testurl=array.getJSONObject(j).getString("url");
       			testurl=testurl.substring(2,testurl.length()-2);
       			testurl=testurl.replaceAll("\\\\", "");
       			urls+=testurl+"/240,";
       		}
       		if(array.length()>0)
       			urls=urls.substring(0,urls.length()-1);
       		
       		}
       		map.put("images", urls);
               listArrays.add(map);  
               
           }  
           return listArrays;
       } catch (JSONException e) {  
           // TODO Auto-generated catch block  
           System.out.println(f+e.getMessage());  
       }   
	   return null;
   }
   
 

  
  public static String processUrl(String text)
  {
	  String regx="<a[\\s]href=[^>]+>";
	  text=text.replaceAll(regx, " ");
	  regx="</a>";
	  text=text.replaceAll(regx, " ");
	  return text;
  }
   
  
   public static HashMap<String, String> JsonToHash(String json)
	{
		 JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(json);
			Iterator<String> itKeys = jsonObject.keys();
	         HashMap<String, String> map = new HashMap<String, String>();  
	         String key="";
	         while(itKeys.hasNext())
	         {
	      	   key=(String)itKeys.next();
	      	   map.put(key,jsonObject.getString(key));
	         }
	         return map;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
         return null;
	}
   
   public static void JsonAddToHash(HashMap<String, String> map, String json)
 	{
 		 JSONObject jsonObject;
 		try {
 			jsonObject = new JSONObject(json);
 			Iterator<String> itKeys = jsonObject.keys(); 
 	         String key="";
 	         while(itKeys.hasNext())
 	         {
 	      	   key=(String)itKeys.next();
 	      	   map.put("user"+key,jsonObject.getString(key));
 	         }
 		} catch (JSONException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} 
 	}
   public static boolean isJson(String value) {  
       try {  
           new JSONObject(value);  
       } catch (JSONException e) {  
           return false;  
       }  
       return true;  
   }  
}



